<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Util extends CI_Controller {

    public function index()
    {
        $this->load->view('login');
    }
    function login_process()
    {
        $email = (!empty($_POST['email'])) ? $this->input->post('email') : '';
        $pwd = (!empty($_POST['password'])) ? $this->input->post('password') : '';

        //mencegah ada yang langsung ke url proses_login, jenis error 0
        if (empty($email) || empty($pwd)) {
            redirect(base_url() . 'util/error_display');
        }
        $query_peserta = $this->db->query('select * from user where EMAIL="'.$email.'" and PASSWORD="'.$pwd.'"')->result_array();
        if (count($query_peserta) != 1) {
            //error jika ada kesalahan login, jenis error 1
            redirect(base_url() . 'util/error_display');
        } else {
            $data_session = array(
                'id' => $query_peserta[0]['ID_USER'],
                'name' => $query_peserta[0]['NAME'],
                'role' => $query_peserta[0]['ID_ROLE'],
                'login' => true,
            );
            $this->session->set_userdata($data_session);
            redirect(base_url() . 'member/');
        }  

    }
    function register_process() {
        $name = (!empty($_POST['nama'])) ? $this->input->post('nama') : '';
        $date = (!empty($_POST['tanggal'])) ? $this->input->post('tanggal') : '';
        $month = (!empty($_POST['bulan'])) ? $this->input->post('bulan') : '';
        $year = (!empty($_POST['tahun'])) ? $this->input->post('tahun') : '';
        $address = (!empty($_POST['alamat'])) ? $this->input->post('alamat') : '';
        $email = (!empty($_POST['email'])) ? $this->input->post('email') : '';
        $pwd1 = (!empty($_POST['password1'])) ? $this->input->post('password1') : '';
        $pwd2 = (!empty($_POST['password2'])) ? $this->input->post('password2') : '';
        
        //mencegah ada yang langsung ke url proses_login, jenis error 0
        if (empty($name) || empty($date) || empty($month) || empty($year) ||
                 empty($address) || empty($pwd2) || empty($pwd1)) {
            redirect(base_url() . 'util/error_display');
        }
        $user = $this->db->query('select COUNT(ID_USER) as jumlah from user')->result_array();
        $data = array(
            'ID_USER' =>$user[0]['jumlah'],
            'ID_ROLE' => '1',
            'NAME' => $name,
            'EMAIL' => $email,
            'PASSWORD' => $pwd1,
            'AVATAR' => base_url().'user/avatars/default_avatar.png'
        );
        $this->db->insert('user', $data);
        $user = $this->db->query('select * from user')->result_array();
        $data_session = array(
            'id' => $user[count($user)-1]['ID_USER'],
            'name' => $user[count($user)-1]['NAME'],
            'role' => $user[count($user)-1]['ID_ROLE'],
            'login' => true,
        );
        $this->session->set_userdata($data_session);
        redirect(base_url() . 'member/');
          
    }
    function logout() {
        $this->session->sess_destroy();
        redirect(base_url() . 'home');
    }    
    function error_display($jenis = 0) {
        $display['message_utama'] = '';
        $display['message_penjelasan'] = '';
        switch ($jenis) {
            case 0:
                $display['message_utama'] = 'Anda harus login terlebih dahulu';
                break;
            case 1:
                $display['message_utama'] = 'Ada kesalahan pada username atau password anda';
                break;
            case 2:
                $display['message_utama'] = 'Sekarang tidak ada SF yang tersedia ' . $jam_sekarang = date('H.i', time());
                ;
                $display['message_penjelasan'] = 'Silakan isi SF pada sesi yang aktif';
                break;
            case 3:
                $display['message_utama'] = 'Anda sudah mengisi SF untuk sesi ini';
                $display['message_penjelasan'] = 'Silakan isi SF pada sesi yang lain';
                break;
            case 4:
                $display['message_utama'] = 'Course yang dipilih tidak ada datanya';
                $display['message_penjelasan'] = 'Silakan isi terlebih dahulu datanya';
                break;
        }

        $this->load->view('header');
        $this->load->view('error_display', $display);
        $this->load->view('footer');
    }
    
    function send_message(){
        $kepada = $_POST['kepada'];
        $subjek = $_POST['subjek'];
        $isi = $_POST['isi'];
        $sender = $this->session->userdata('id');
        
        $this->load->model('schedule/users_model', 'message');
        $receiver_id = $this->message->get_user_id($kepada);
        
        $this->load->helper('date');
        $message = array(
            'ID_FROM' => $sender,
            'ID_TO' => $receiver_id['ID_USER'],
            'CONTENT' => $isi,
            'TIME' => unix_to_human(time(), TRUE, 'us'),
            'SUBJECT' => $subjek,
            'STATUS' => '1'
        );
        
        $this->message->set_message($message);
        redirect(base_url() . 'member/to_message');
    }
}
